package com.geeksaga.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.geeksaga.common.util.CacheUtil;
import com.geeksaga.dao.CategoryDao;
import com.geeksaga.domain.SagaCategory;

/**
 * @author newbie
 * @since 0.1
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    
    @Autowired private CategoryDao categoryDao;

    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#add(com.geeksaga.domain.BbsCategory)
     */
    public SagaCategory add(SagaCategory bbsCategory) throws DataAccessException {
        return categoryDao.add(bbsCategory);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#loadCache()
     */
    public void loadCache() throws DataAccessException {
        List<SagaCategory> list = categoryDao.list();
        
        Map<String, Map<String, String>> cacheMap = new HashMap<String, Map<String, String>>();
        
        String bbsId = null;
        Map<String, String> temp = null;
        
        for(SagaCategory category : list) {
        	if(!category.getTargetId().equals(bbsId)) {
        		if(temp != null)
        			cacheMap.put(bbsId, temp);
        		
        		temp = new HashMap<String, String>();
        	}
        	
            temp.put(category.getSid(), category.getCategoryName());
            
            bbsId = category.getTargetId();
        }
        
        cacheMap.put(bbsId, temp);
        
        CacheUtil.put(CacheUtil.CACHE, CacheUtil.KEY_CATEGORY, cacheMap);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#findCacheByTargetId(java.lang.String)
     */
    public Map<String, String> findCacheByTargetId(String targetId) {
        Map<String, Map<String, String>> cacheMap = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_CATEGORY);
        
        if(cacheMap == null) {
            loadCache();
            
            cacheMap = CacheUtil.get(CacheUtil.CACHE, CacheUtil.KEY_CATEGORY);
        }

        return cacheMap.get(targetId);
    }

    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#findCacheByBbsIdToList(java.lang.String)
     */
    public List<SagaCategory> findCacheByBbsIdToList(String bbsId) {
        Map<String, String> cacheMap = findCacheByTargetId(bbsId);

        List<SagaCategory> list = new ArrayList<SagaCategory>();

        if (cacheMap != null) {
            Iterator<Entry<String, String>> iterator = cacheMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> entry = iterator.next();
                list.add(new SagaCategory(entry.getKey(), entry.getValue()));
            }
        }

        return list;
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#findList(java.lang.String)
     */
    public List<SagaCategory> findList(String bbsId) {
        return categoryDao.findList(bbsId);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.CategoryService#findList(java.lang.String)
     */
    public List<SagaCategory> list() throws DataAccessException {
        return categoryDao.list();
    }
}